import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import accuracy_score, classification_report, ConfusionMatrixDisplay
import matplotlib.pyplot as plt

# 读取分类数据集
data = pd.read_csv("src/classification.csv")

# 删除有缺失的样本
data.dropna(inplace=True)

# 特征和标签
X = data.iloc[:, :-1]  # 特征
y = data.iloc[:, -1]   # 标签（分类）

# 划分数据集，80%训练，20%测试
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 使用Fisher线性判别分析进行分类
lda = LinearDiscriminantAnalysis()

# 进行交叉验证
cv_scores = cross_val_score(lda, X_train, y_train, cv=5)  # 5折交叉验证
print(f'Cross-validated accuracy: {cv_scores.mean()}')

# 在训练集上拟合模型
lda.fit(X_train, y_train)

# 预测测试集
y_pred = lda.predict(X_test)

# 评估模型
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')
print(classification_report(y_test, y_pred))

# 绘制混淆矩阵
ConfusionMatrixDisplay.from_estimator(lda, X_test, y_test)
plt.title("Confusion Matrix for Fisher's Linear Discriminant Analysis")
plt.show()
